<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2020-2030 衡水山木枝技术服务有限公司
 * @Date: 2021-07-04 09:54:16
 * @Description: 从自己系统内匹配导出订单金额与成本
 * @LastEditors: 李志刚
 * @LastEditTime: 2021-12-13 08:52:40
 * @FilePath: /qingshanERP/app/Console/Commands/Test/ExportOrderExcel.php
 */

namespace App\Console\Commands\Test;

use App\Exports\ExportDemo;
use App\Models\QingShanERP\GoodSpec;
use App\Models\QingShanERP\GoodSpecSupplier;
use Illuminate\Console\Command;
use App\Models\QingShanERP\Order;

class ExportOrderExcel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'export-order-excel';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '从自己系统内匹配导出订单金额与成本';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        try {
            // 第二家店的几个品 ['863860059849','832286241674','826697154154','826697154155','832286241673']
            // ->where('shop_id', 1)
            $all_order = Order::with('goods', 'pdd','shop')->where('confirm_time', '>', '2021-7-01 00:00:00')->where('confirm_time', '<', '2021-8-01 00:00:00')->get();
            $title   = [
                ['店铺','订单号', '快递单号', '发货时间', '商品', '对应名称', '数量', '进货价', '运费', '成本价', '订单金额', '售后', '标记','备注']
            ];
            $all_good_spec = GoodSpec::where('del_flag', 0)->get();
            $all_good_spec_supplier = GoodSpecSupplier::where('del_flag', 0)->get();
            $excel = [];
            $after = config('pdd.after_sales_status');
            $total_price = 0;
            $total_shipping = 0;
            $total_cb = 0;
            $total_pay = 0;
            foreach ($all_order as $a) {
                $tmp_excel = [];
                // 无商品的订单
                if ($a->goods->count() <= 0) {
                    $tmp_excel[] = [
                            '店铺' => $a->shop->shopname,
                            '订单号' => $a->order_sn,
                            '快递单号' => $a->tracking_number,
                            '发货时间' => $a->shipping_time,
                            '商品' => '',
                            '对应名称' => '',
                            '数量' => '',
                            '进货价' => '',
                            '运费' => '',
                            '成本价' => '',
                            '订单金额' => $a->pay_amount,
                            '售后' => $after[$a->after_sales_status],
                            '标记' => $a->remark_tag_name,
                            '备注' => $a->pdd->remark,
                        ];
                }
                else {
                    foreach ($a->goods as $k => $g) {
                        $good_spec_id = $all_good_spec->where('sku_id', $g->goods_spec)->first()->id;
                        // dump($good_spec_id);
                        dump($a->order_sn . ' - ' . $g->goods_spec . ' : ' . $g->good_spec_name);
                        $good_spec_supplier = $all_good_spec_supplier->where('supplier_id', 2)->where('good_spec_id', $good_spec_id)->first();
                        // if (in_array($g->goods_spec, ['863860059849', '832286241674', '826697154154', '826697154155', '832286241673'])) {
                        //     // 只出 飞
                        //     // continue;
                        //     $good_spec_supplier = $all_good_spec_supplier->where('supplier_id', 2)->where('good_spec_id', $good_spec_id)->first();
                        // } else {
                        //     // 只出 光
                        //     continue;
                        //     $good_spec_supplier = $all_good_spec_supplier->where('supplier_id', 1)->where('good_spec_id', $good_spec_id)->first();
                        // }
                        if (is_null($good_spec_supplier)) {
                            dump($g->goods_name . ' -> ' . $g->good_spec_name . '，没有找到对应的供应商');
                        }
                        dump('供货商名称：' . optional($good_spec_supplier)->spec);
                        $tmp_price = optional($good_spec_supplier)->price * $g->goods_count;
                        $tmp_shipping = optional($good_spec_supplier)->shipping * $g->goods_count;
                        $tmp_cb = $tmp_price + $tmp_shipping;
                        if (count($tmp_excel) == 0) {
                            $tmp_excel[] = [
                                '店铺' => $a->shop->shopname,
                                '订单号' => $a->order_sn,
                                '快递单号' => $a->tracking_number,
                                '发货时间' => $a->shipping_time,
                                '商品' => $g->goods_name . '>' . $g->good_spec_name,
                                '对应名称' => optional($good_spec_supplier)->spec,
                                '数量' => $g->goods_count,
                                '进货价' => $tmp_price,
                                '运费' => $tmp_shipping,
                                '成本价' => $tmp_cb,
                                '订单金额' => $a->pay_amount,
                                '售后' => $after[$a->after_sales_status],
                                '标记' => $a->remark_tag_name,
                                '备注' => $a->pdd->remark,
                            ];
                            $total_pay = $total_pay + $a->pay_amount;
                        } else {
                            $tmp_excel[] = [
                                '店铺' => '',
                                '订单号' => '',
                                '快递单号' => '',
                                '发货时间' => '',
                                '商品' => $g->goods_name . '>' . $g->good_spec_name,
                                '对应名称' => optional($good_spec_supplier)->spec,
                                '数量' => $g->goods_count,
                                '进货价' => $tmp_price,
                                '运费' => $tmp_shipping,
                                '成本价' => $tmp_cb,
                                '订单金额' => 0,
                                '售后' => '',
                                '标记' => '',
                                '备注' => '',
                            ];
                        }
                        $total_price = $total_price + $tmp_price;
                        $total_shipping = $total_shipping + $tmp_shipping;
                    }
                }
                if (count($tmp_excel) > 0) {
                    foreach ($tmp_excel as $t) {
                        $excel[] = $t;
                    }
                }
            }
            $total_cb = $total_price + $total_shipping;
            $excel[] = [
                '店铺' => '',
                '订单号' => '',
                '快递单号' => '',
                '发货时间' => '',
                '商品' => '',
                '对应名称' => '',
                '数量' => '',
                '进货价' => $total_price,
                '运费' => $total_shipping,
                '成本价' => $total_cb,
                '订单金额' => $total_pay,
                '售后' => '',
                '标记' => '',
                '备注' => '',
            ];
            // dump($excel);
            \Maatwebsite\Excel\Facades\Excel::store(new ExportDemo($title, $excel), '/export/11月订单对比数据-' . date('Y-m-d H:i:s') . '.xlsx', 'local');
            dd('success');
        } catch (\Throwable $th) {
            dd($th->getFile().$th->getLine().' - '.$th->getMessage());
        }
    }
}
